package com.turing.manage.baojian;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class BaoJianServlet extends HttpServlet {
	
	BaoJianService courseService = new BaoJianService();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			String method = request.getParameter("method");
			
			if ("query".equals(method))
			{
				this.query(request,response);
			}
			else if ("addPage".equals(method)) {
				this.addPage(request,response);
			}
			else if ("add".equals(method)) {
				this.add(request,response);
			}
			else if ("editPage".equals(method)) {
				this.editPage(request,response);
			}
			else if ("edit".equals(method)) {
				this.edit(request,response);
			}
			else if ("delete".equals(method)) {
				this.delete(request,response);
			}
		} catch (ClassNotFoundException | SQLException e) {
			System.out.println("1.发送的异常信息为:"+e.getMessage());
			System.out.println("2.发送异常的内存地址为:"+e.getStackTrace());
			System.out.println("3.转向到错误处理页面");
			request.setAttribute("errorMsg",e.getMessage());
			request.getRequestDispatcher("/manage/error.jsp").forward(request, response);
		}
	}

	
	
	
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, FileNotFoundException, SQLException, IOException {
				String[] delids = request.getParameterValues("delIdArray");
				for (String everydelids : delids) {
					System.out.println(everydelids);
				}
				//2.调用M层中的方法实现删除
				courseService.deleteBaojianByIds(delids);
				//3.重定向
				response.sendRedirect(request.getContextPath()+"/manage/baojian.do?method=query");
		
	}




	/**
	 *  desc 修改方法
	 * @param request
	 * @param response
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException {

		String room_id = request.getParameter("room_id");
		String room_name = request.getParameter("room_name");
		String room_size = request.getParameter("room_size");
		
		courseService.updateBaojian(room_id,room_name,room_size);
		
		response.sendRedirect(request.getContextPath()+"/manage/baojian.do?method=query");
		
	}



	/**
	 *  desc  进入修改页面
	 * @param request
	 * @param response
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void editPage(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
			
		String room_id = request.getParameter("room_id");
		 
		Map<String, Object> map=courseService.queryBaojianById(room_id);
		
		request.setAttribute("map",map);
		
		request.getRequestDispatcher("/manage/baojian/edit.jsp").forward(request, response);
		
	}


	/**
	 *  desc 执行添加方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws SQLException 
	 * @throws FileNotFoundException 
	 * @throws ClassNotFoundException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, FileNotFoundException, SQLException, IOException {
		String course_name = request.getParameter("room_name");
		String course_time = request.getParameter("room_size");
		

		courseService.addCourse(course_name,course_time);
		
		response.sendRedirect(request.getContextPath()+"/manage/baojian.do?method=query");
		System.out.println("执行完毕");
		
	}



	/**
	 *  desc  跳转到添加页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void addPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				//跳转
		request.getRequestDispatcher("/manage/baojian/add.jsp").forward(request, response);
		
	}

	/**
	 *  desc 查询全部  ok
	 * @param request
	 * @param response
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void query(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
		
		List<Map<String,Object>> list = courseService.queryCourseAll();
		
		request.setAttribute("list", list);
		
		request.getRequestDispatcher("/manage/baojian/list.jsp").forward(request, response);
		
	}
	
	
}
